package history.tencent1.problem5;

import java.util.Arrays;
import java.util.Scanner;

/**
 *
 */

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        sc.nextInt();
        while (sc.hasNext()){
            int n = sc.nextInt();
            int m = sc.nextInt();
            int[] w = new int[n];
            for(int i = 0; i < n; i++){
                w[i] = sc.nextInt();
            }
            System.out.println("w" + Arrays.toString(w));

            System.out.println(minToLeft(w, m));
        }
    }
    public static int minToLeft(int[] w, int m){
        int n = w.length;
        Arrays.sort(w);
        int[] preSum = new int[n + 1];
        int sum = 0;
        preSum[0] = 0;
        for(int i = 0; i < n; i++){
            preSum[i + 1] = preSum[i] + w[i];
            sum += w[i];
        }
        if(sum % m == 0){
            return 0;
        }
        System.out.println(Arrays.toString(preSum));
        for(int i = 1; i < n + 1; i++){
            for(int j = i - 1; j >= 0; j--){
                if((preSum[i] - preSum[j]) % m == (sum % m)){
                    return  preSum[i] - preSum[j];
                }
            }
        }
        return sum;
    }
}
